package web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Operator;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import service.EmployeeS;

import service.AttendanceS;

public class AttendanceW implements Controller {

	private String viewName;
	private int pageSize;
	private AttendanceS attendanceS;
	private EmployeeS employeeS;
	
	public String getViewName() {return viewName;}
	public void setViewName(String viewName) {this.viewName = viewName;}public int getPageSize() {return pageSize;}
	public void setPageSize(int pageSize) {this.pageSize = pageSize;}
	public AttendanceS getAttendanceS() {return attendanceS;}
	public void setAttendanceS(AttendanceS attendanceS) {this.attendanceS = attendanceS;}
	public EmployeeS getEmployeeS() {return employeeS;}
	public void setEmployeeS(EmployeeS employeeS) {this.employeeS = employeeS;}
	
	public ModelAndView handleRequest(HttpServletRequest request,
			                          HttpServletResponse response) throws Exception
    {
	
		ModelAndView mav=new ModelAndView();
		List model=new ArrayList();
		int dom=0000;
		if(null==request.getParameter("dom"))
		{
			mav.setViewName(viewName+"book");
			List eList=employeeS.getStaff();
			model.add(eList);
		    mav.addObject("model",model);
			return mav;
		}
		else dom=Integer.parseInt(request.getParameter("dom"));
		
		if(2==dom)
		{
		String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"GBK");
        String[] values=name.split(",");
        response.setCharacterEncoding("utf-8");
        String  aaa=attendanceS.attendance(values[1],values[0]);
        System.out.println(aaa);
        response.getWriter().print(aaa);
        return null;
		}
		
		Operator operator=(Operator)request.getSession().getAttribute("operator");
		if(null==operator)
		{ 
			
		}
		switch (dom)
		{
		case 1011:{
                    mav.setViewName(viewName+"index");
			
			        int count=employeeS.getCount("","");
			        int pageCount=(count+pageSize-1)/pageSize;
			  
			        List ePage=employeeS.getPage(count,pageSize,1,"","");
			        List aPage=new ArrayList();
			        List mPage=new ArrayList();
			        for(int i=0;i<ePage.size();i++)
			        {
			           Map employee=(Map)ePage.get(i);
			           aPage.add(attendanceS.getWeekAttendance((String)employee.get("enumber")));
			           mPage.add(attendanceS.getMonthStat((String)employee.get("enumber")));
			        }
			
			        model.add(count);
			        model.add(pageCount);
			        model.add(1);
			        model.add(pageSize);
			        model.add(ePage);
			        model.add(aPage);
			        model.add(mPage);
			  
        			mav.addObject("model",model);
		}
		case 4011:{
		
		    mav.setViewName(viewName+"view");
		    
		    String number=request.getParameter("number");
		    
		    int year=0;
		    int month=0;
		    if(null!=request.getParameter("year")&&null!=request.getParameter("month"))
		    {  year=Integer.parseInt(request.getParameter("year"));
	     	   month=Integer.parseInt(request.getParameter("month"));
		    }
		    else
		    {  Calendar c=Calendar.getInstance();
		       year=c.get(Calendar.YEAR);
		       month=c.get(Calendar.MONTH)+1;
		    }
		    
		    List mList=attendanceS.getMonth(number,year,month);
		    List ymList=attendanceS.getYMofHave(number);
		    Map employee=employeeS.getEmployee(number);
		    List sList=attendanceS.getOneMonthStat(number,year,month);
		    
		    
		    model.add(mList);
		    model.add(employee);
		    model.add(year);
		    model.add(month);
		    model.add(ymList);
		    model.add(number);
		    model.add(sList);
		    
		    mav.addObject("model",model);
		    
		}
		case 5001:
		{
		    mav.setViewName(viewName+"edit");
		    
		    String number=request.getParameter("number");
		    
		    int year=0;
		    int month=0;
		    if(null!=request.getParameter("year")&&null!=request.getParameter("month"))
		    {  year=Integer.parseInt(request.getParameter("year"));
	     	   month=Integer.parseInt(request.getParameter("month"));
		    }
		    else
		    {  Calendar c=Calendar.getInstance();
		       year=c.get(Calendar.YEAR);
		       month=c.get(Calendar.MONTH)+1;
		    }
		    
		    List mList=attendanceS.getMonth(number,year,month);
		    List ymList=attendanceS.getYMofHave(number);
		    Map employee=employeeS.getEmployee(number);
		    
		    
		    model.add(mList);
		    model.add(employee);
		    model.add(year);
		    model.add(month);
		    model.add(ymList);
		    model.add(number);
		    
		    mav.addObject("model",model);
		    
		}
		case 15:{
			String aid=request.getParameter("id");
			String fill=request.getParameter("fill");
			String value=new String(request.getParameter("value").getBytes("ISO-8859-1"),"GBK");
			
			attendanceS.updateA(aid, fill, value);
	
		}
		case 24:
		{
		    mav.setViewName(viewName+"view2");
		    
		    String number=request.getParameter("number");
		    
		    int year=0;
		    int month=0;
		    if(null!=request.getParameter("year")&&null!=request.getParameter("month"))
		    {  year=Integer.parseInt(request.getParameter("year"));
	     	   month=Integer.parseInt(request.getParameter("month"));
		    }
		    else
		    {  Calendar c=Calendar.getInstance();
		       year=c.get(Calendar.YEAR);
		       month=c.get(Calendar.MONTH)+1;
		    }
		    
		    List mList=attendanceS.getMonth(number,year,month);
		    List ymList=attendanceS.getYMofHave(number);
		    Map employee=employeeS.getEmployee(number);
		    
		    
		    model.add(mList);
		    model.add(employee);
		    model.add(year);
		    model.add(month);
		    model.add(ymList);
		    model.add(number);
		    
		    mav.addObject("model",model);
	
		}
		case 34:
		{
			mav.setViewName(viewName+"attendance");
			
			int count=employeeS.getCount("","");
			int pageCount=(count+pageSize-1)/pageSize;
			  
			List ePage=employeeS.getPage(count,20,1,"","");
			List aPage=new ArrayList();
			for(int i=0;i<ePage.size();i++)
			{
			    Map employee=(Map)ePage.get(i);
			    aPage.add(attendanceS.getThreeAttendance((String)employee.get("enumber")));
			}
			model.add(count);
			model.add(pageCount);
			model.add(1);
			model.add(pageSize);
			model.add(ePage);
			model.add(aPage);
			  
			mav.addObject("model",model);
			
		}
	}
	return mav;	
    }

}